package guava.collection;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

import java.util.Iterator;

/**
 * MultiSetTest
 *
 * @author nanxin.chen
 * @date 2015/9/7
 */
public class MultiSetTest {
    public static void main(String[] args) {
        Multiset<String> multiset = HashMultiset.create();
        multiset.add("a");
        multiset.add("b");
        multiset.add("c");
        multiset.add("d");
        multiset.add("a");
        multiset.add("b");
        multiset.add("c");
        multiset.add("b");
        multiset.add("b");
        multiset.add("b");

        //print the occurrence of an element
        System.out.println("count b:" + multiset.count("b"));

        System.out.println("total size:" + multiset.size());
        //get distinct elements of the multiset as set
        System.out.println("distinct elements:" + multiset.elementSet());

        System.out.println("iterator multiset:");
        Iterator<String> iterator = multiset.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        for (Multiset.Entry<String> entry : multiset.entrySet()) {
            System.out.println("element:" + entry.getElement() + ",  count:" + entry.getCount());
        }

        //remove extra occurrences
        multiset.remove("b",2);
        //print the occurrence of an element
        System.out.println("Occurence of 'b' : "+multiset.count("b"));

        multiset.remove("b");
        System.out.println("Occurence of 'b' : "+multiset.count("b"));
    }
}
